package com.entrogy.controller;


import com.entrogy.bean.AttendanceGroupEntity;
import com.entrogy.bean.AttendancePeople;
import com.entrogy.bean.PageBean;
import com.entrogy.service.AttendanceGroupService;
import com.entrogy.service.AttendancePeopleService;
import com.entrogy.utils.ApiReturnUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;


/**考勤组管理控制层
 * @Author 许红松
 * @date 2019/5/9 0009 下午 4:33
 */
@RestController
@Transactional
@RequestMapping(value = "/attendanceGroup")
@Api(description = "对考勤组管理信息操作的相关接口")
public class AttendanceGroupController {


    @Autowired
    private AttendanceGroupService attendanceGroupService;//考勤组管理信息service接口
    @Autowired
    private AttendancePeopleService attendancePeopleService;//考勤人员service接口

    /**
     *   考勤组管理分页
     * @return List
     */
    @ApiOperation(value = "分页获取考勤组管理信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "school_code",dataType = "int",paramType = "query",required = true),
            @ApiImplicitParam(value = "考勤组名称",name = "AttendanceName",dataType = "String",paramType = "query",required = false),
            @ApiImplicitParam(value = "当前页数",name="page",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "total",dataType = "int",paramType = "query",required = true)
    })
    @RequestMapping(value = "/getAttendanceGroupList",method = RequestMethod.POST)
    public List<Map<String,Object>> getAttendanceGroupList(@RequestParam(value="currentPage")Integer  currentPage, @RequestParam(value="pageSize")Integer  pageSize, @RequestParam(value="school_code")Integer school_code, String AttendanceName ){
        PageHelper.startPage(currentPage,pageSize);
        Map<String,Object> map=new HashMap<>();
        map.put("school_code",school_code);
        if ( AttendanceName!=null && !"".equals(AttendanceName)){
            map.put("AttendanceName",AttendanceName);
        }
        List<Map<String,Object>> list=attendanceGroupService.getAttendanceGroupList(map);
        return list;
    }

    /**
     *   新增考勤组管理信息
     * @param school_code
     * @param attendanceGroupEntity
     * @return
     */
    @ApiOperation(value = "新增考勤组管理")
    @RequestMapping(value = "/addAttendanceGroup",method = RequestMethod.POST)
    public Map<String,Object> addAttendanceGroup(@ModelAttribute("attendanceGroupEntity") AttendanceGroupEntity attendanceGroupEntity, @RequestParam(value="school_code")Integer school_code,String[] persions,String[] persionNames){
        attendanceGroupEntity.setSchool_code(school_code.toString());
        List<String> list1=null;
        List<String> list2=null;
        List<String> list3=null;
        List<AttendancePeople> list=new ArrayList<>();
        if (persions!=null){
            int  psize=persions.length;
            attendanceGroupEntity.setAttendanceSize(psize);
            list1 = Arrays.asList(persions);
            list2 = Arrays.asList(persionNames);
        }
        int  code1=attendanceGroupService.addAttendanceGroup(attendanceGroupEntity);
        if (code1==1){
            int attendanceId=attendanceGroupEntity.getAttendanceId();
            for (String tString : list1) {
                AttendancePeople attendancePeople=new AttendancePeople();
                attendancePeople.setAttendanceId(attendanceId);
                attendancePeople.setSchool_code(Integer.parseInt(attendanceGroupEntity.getSchool_code()));
                attendancePeople.setUserId(tString);
                list.add(attendancePeople);
            }
        }
        int  code=attendancePeopleService.addAttendancePeople(list);
        if (code==2){
            return ApiReturnUtil.success("新增成功","");
        }else{
            return ApiReturnUtil.success("新增失败","");
        }


    }

    /**
     *   修改考勤组管理信息
     * @return
     */
    @ApiOperation(value = "修改考勤组管理")
    @RequestMapping(value = "/updateAttendanceGroup",method = RequestMethod.POST)
    public Map<String,Object> updateAttendanceGroup(@ModelAttribute("attendanceGroupEntity") AttendanceGroupEntity attendanceGroupEntity,@RequestParam(value="school_code")Integer school_code){
        attendanceGroupEntity.setSchool_code(school_code+"");
        attendanceGroupEntity.setAttendanceName(attendanceGroupEntity.getAttendanceName());
        int  code=attendanceGroupService.updateAttendanceGroup(attendanceGroupEntity);
        if (code==1){
            return ApiReturnUtil.success("修改成功","");
        }else{
            return ApiReturnUtil.success("修改失败","");
        }


    }


    /**
     *   删除考勤组管理信息
     * @return
     */
    @ApiOperation(value = "删除考勤组管理信息")
    @RequestMapping(value = "/deleteAttendanceGroup",method = RequestMethod.POST)
    public Map<String,Object> deleteAttendanceGroup(Integer school_code,int  attendanceId){
        if (school_code==null|| school_code==0){
            return ApiReturnUtil.error("您无操作权限！");
        }
        int  code= attendanceGroupService.deleteAttendanceGroup(attendanceId);
        if(code==1){
            attendancePeopleService.deleteAttendancePeople(String.valueOf(attendanceId));
            return ApiReturnUtil.success("删除成功","");
        }
        return ApiReturnUtil.error("删除失败");
    }



    /**
     *  考勤组中查询被考勤人员List分页
     * @return List
     */
    @ApiOperation(value = "分页获取所有教师List信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "学校ID",name = "schoolCode",dataType = "int",paramType = "query",required = false),
            @ApiImplicitParam(value = "教师姓名",name = "name",dataType = "String",paramType = "query",required = false),
            @ApiImplicitParam(name="page",value = "当前页数",required = false,dataType = "Integer",paramType = "query"),
            @ApiImplicitParam(value = "每页条数",name = "total",dataType = "int",paramType = "query",required = false)
    })
    @RequestMapping(value = "/getUserList",method = RequestMethod.POST)
    public PageBean<Map<String,Object>> getUserList(Integer  currentPage, Integer  pageSize, Integer schoolCode, String name ){
        Map<String,Object> map=new HashMap<>();
        if(schoolCode!=null&& schoolCode>0){
            map.put("school_code",schoolCode);
        }
        if ( name!=null && !"".equals("name")){
            map.put("name",name);
        }
        int total2=attendanceGroupService.getUserListTotal(map);
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> list=attendanceGroupService.getUserList(map);
        return new PageBean<Map<String,Object>>().pageBeans(list,total2);
    }
}
